package cn.iocoder.yudao.module.trade.mq.consumer;

import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
import cn.iocoder.yudao.module.infra.util.TraceIdUtil;
import cn.iocoder.yudao.module.trade.mq.message.OrderDeliveryMessage;
import cn.iocoder.yudao.module.trade.service.order.OrderDeliveryService;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 针对 {@link OrderDeliveryMessage} 的消费者
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class OrderDeliveryConsumer {

    @Resource
    private OrderDeliveryService orderDeliveryService;

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(OrderDeliveryMessage message) {
        TraceIdUtil.setTraceId(TracerUtils.getTraceId());
        log.info("[onMessage][消息内容({})]", message);
        orderDeliveryService.delivery(message.getOrder(), message.getOrderItems());
        TraceIdUtil.removeTraceId();
    }

}
